﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mDashboard.Common
{
    public class MailHelper
    {
        public static void SendMailUseGmail(string subject, string sums, string[] strAdress, string[] strCC)
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            foreach (string str in strAdress)
            {
                msg.To.Add(str);
            }
            if (strCC != null)
            {
                foreach (string str in strCC)
                {
                    msg.CC.Add(str);
                }
            }
            //抄送人增加自己
            msg.CC.Add(Lamex.Common.Loginer.CurrentUser.Email);
            /*   
            * msg.CC.Add("c@c.com");   
            * msg.CC.Add("c@c.com");可以抄送给多人   
            */
            msg.From = new MailAddress(Lamex.Common.Loginer.CurrentUser.Email, "", System.Text.Encoding.UTF8);
            /* 上面3个参数分别是发件人地址，发件人姓名，编码*/
            msg.Subject = "喷粉生产力" + subject + "";//邮件标题    
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码       
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码    
            msg.IsBodyHtml = true;//是否是HTML邮件    
            msg.Priority = MailPriority.Normal;//邮件优先级    

            string MailBody = "TY,";
            MailBody += "<br>";
            MailBody += "<div>" + "&nbsp;&nbsp;&nbsp" + subject + "喷粉线生产力为";
            MailBody += "<span style=\"white-space: nowrap;font-size: 20pt;line-height:15px;background-color:yellow; \" > " + sums + "</span>";
            MailBody += "请登录到MES系统（报表>生产-MCG>喷粉绩效生产力）模块进行审批，谢谢！</div><br>";

            //增加署名
            MailBody += "<br>";
            MailBody += "Best Regards,";
            MailBody += "<br>";
            MailBody += Lamex.Common.Loginer.CurrentUser.AccountName;
            MailBody += "<br>";
            MailBody += "联系电话：" + Lamex.Common.Loginer.CurrentUser.Tel;
            msg.Body = MailBody;



            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential("mailsender@lamex.com.hk", "Bl@7701167//");
            //client.Credentials = new System.Net.NetworkCredential("Bill.Luo@lamex.com.hk", "");
            client.Host = "172.23.134.118";
            // client.EnableSsl = true;//经过ssl加密    
            object userState = msg;
            try
            {
                client.Send(msg);
                GC.Collect();

            }
            catch (System.Net.Mail.SmtpException ex)
            {
                MessageBox.Show(ex.Message, "发送邮件出错");
            }
        }
        public static void SendMailUseGmail_Approval(string subject, string file, string[] strAdress, string[] strCC)
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            //获取第一个收件人
            string firstAdress = strAdress[0].Split('@')[0].Replace(".", " ");
            foreach (string str in strAdress)
            {
                msg.To.Add(str);
            }
            if (strCC != null)
            {
                foreach (string str in strCC)
                {
                    msg.CC.Add(str);
                }
            }
            //抄送人增加自己
            msg.CC.Add(Lamex.Common.Loginer.CurrentUser.Email);
            /*   
            * msg.CC.Add("c@c.com");   
            * msg.CC.Add("c@c.com");可以抄送给多人   
            */
            msg.From = new MailAddress(Lamex.Common.Loginer.CurrentUser.Email, "", System.Text.Encoding.UTF8);
            /* 上面3个参数分别是发件人地址，发件人姓名，编码*/
            msg.Subject = "喷粉生产力" + subject + "";//邮件标题    
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码       
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码    
            msg.IsBodyHtml = true;//是否是HTML邮件    
            msg.Priority = MailPriority.Normal;//邮件优先级    

            //附件
            Attachment att = null;
            if (File.Exists(file))
            {
                att = new Attachment(file);
                msg.Attachments.Add(att);
                //释放流
                FileStream fs = File.OpenRead(file);//打开文件读取
                fs.Flush();
                fs.Dispose();
                fs.Close();//释放流

            }


            string MailBody = firstAdress + ",";
            MailBody += "<br>";
            MailBody += "<br>";
            MailBody +=  "&nbsp;&nbsp;&nbsp" + subject + "喷粉线生产力已审批,详情参考附件。<br>";

            //增加署名
            MailBody += "<br>";
            MailBody += "Best Regards,";
            MailBody += "<br>";
            MailBody += Lamex.Common.Loginer.CurrentUser.AccountName;
            MailBody += "<br>";
            MailBody += "联系电话：" + Lamex.Common.Loginer.CurrentUser.Tel;
            msg.Body = MailBody;



            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential("mailsender@lamex.com.hk", "Bl@7701167//");
            //client.Credentials = new System.Net.NetworkCredential("Bill.Luo@lamex.com.hk", "");
            client.Host = "172.23.134.118";
            // client.EnableSsl = true;//经过ssl加密    
            object userState = msg;
            try
            {
                client.Send(msg);
                GC.Collect();

            }
            catch (System.Net.Mail.SmtpException ex)
            {
                MessageBox.Show(ex.Message, "发送邮件出错");
            }
        }

    }
}
